Ext.define('ZYY.controller.Viewport', {
    extend : 'Ext.app.Controller',
    //			stores : ['system.menuTree'],
    views : ['viewport.header','viewport.buttom','viewport.center','viewport.left'],
    refs : [
    {
        ref: 'center', 
        selector: 'center'
    }
    ],
    init : function(application) {
				
        if (this.inited) {
            return;
        }
        this.inited = true;
        this.control({
            'left treepanel' : {
                itemclick : this.menuClick
            },
            'navhead panel toolbar #closeAll':{
                click:function(){
                    var main=this.getCenter();
                    main.items.each(function(item){
                        if(item.closable)
                            main.remove(item);
                    });
                }
            },
            'navhead panel toolbar #logout':{
                click:function(){
                    window.location.href=path+"/j_spring_security_logout";
                }
            }
        });
        console.log('控制器' + this.$className + '初始化');
    },
    menuClick : function(view, record) {
        if (record.data.leaf) {
            this.application.runAction(record.data.id, 'Index');
        //					var panel = Ext.getCmp(record.id);
        //					if (panel) {
        //						this.getCenter().setActiveTab(panel);
        //					} else {
        //						panel = {
        //							xtype : record.data.id,
        //							title : record.data.text + '&nbsp;',
        //							closable : true
        //						}, this.openTab(panel, record.data.id);
        //					}
        } else {
            if (record.get('expanded')) {
                view.collapse(record);
            } else {
                view.expand(record);
            }
        }
    },
    openTab : function(panel, id) {
        this.application.runAction(id);
        console.log("OpenTab:" + id);
        var main = this.getCenter();
        var tab = main.getComponent(id);
        if (tab) {
            main.setActiveTab(tab);
        } else {
            panel.id = id;
            main.add(panel).show();
        }
    }
});